package com.guoguo.controller;


import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Images;
import com.guoguo.entity.Recharge;

import com.guoguo.entity.vo.RechargeVO;
import com.guoguo.service.IRechargeService;
import com.guoguo.utils.UUIDUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by KLSD on 2017/9/29.
 */
@Controller
@RequestMapping("/view")
public class RechargeController extends BaseController {
    @Autowired
    @Resource
    private IRechargeService iRechargeService;

    @RequestMapping(value = "/rechargeList", method = RequestMethod.GET)
    public ModelAndView recharge(@RequestParam int pageNumber,@RequestParam int pageSize) {
        Map<String, Object> data = new HashMap<>();
        PageBean<RechargeVO> rechargeVo = iRechargeService.selectRechargeList(pageNumber,pageSize);
        data.put("rechargeList", rechargeVo);
        return returnView("/rechargeList", data);


    }


    /**
     * 跳转添加修改支付类型
     * @param rechId
     * @return
     */
    @RequestMapping(value="/saveRecharge",method = RequestMethod.GET)
    public ModelAndView Recharge(String rechId){
        Map<String,Object> data=new HashMap();
        Map<Integer,String> recharges=new TreeMap();
        recharges.put(0,"积分充值");
        recharges.put(1,"会员充值");
        data.put("rechTypeList",recharges);
        Map<Integer,String> times=new TreeMap();
        times.put(0,"0");
        times.put(1,"1");
        times.put(2,"2");
        times.put(3,"3");
        times.put(4,"4");
        times.put(5,"5");
        times.put(6,"6");
        times.put(7,"7");
        times.put(8,"8");
        times.put(9,"9");
        times.put(10,"10");
        times.put(11,"11");
        times.put(12,"12");
        times.put(13,"13");
        times.put(14,"14");
        times.put(15,"15");
        times.put(16,"16");
        times.put(17,"17");
        times.put(18,"18");
        times.put(19,"19");
        times.put(20,"20");
        times.put(21,"21");
        times.put(22,"22");
        times.put(23,"23");
        times.put(24,"24");
        data.put("timesList",times);
        if ("".equals(rechId) || rechId == null) {
            data.put("rechId",UUIDUtils.getUUID());

        } else {
            Recharge recharge=iRechargeService.selectByPrimaryKey(rechId);
            data.put("rechId",recharge.getRechId());
            data.put("rechType",recharge.getRechType());
            data.put("money",recharge.getMoney());
            data.put("point",recharge.getPoint());
            data.put("timeLimit",recharge.getTimeLimit());
            data.put("frequency",recharge.getFrequency());
            data.put("sort",recharge.getSort());

        }
        return  returnView("/recharge/rechargeAdd",data);


    }
    //添加/修改支付类型
    @ResponseBody
    @RequestMapping(value = "/saveOrUpdateRecharge", method = RequestMethod.POST)
    public JSONObject saveRecharge(@RequestParam String rechId,@RequestParam String money,@RequestParam int rechType,
                                   @RequestParam int point,@RequestParam int timeLimit,@RequestParam int frequency,@RequestParam int sort){
        //作者id
        Recharge recharge = iRechargeService.addRecharge(rechId,money,rechType,point,timeLimit,frequency,sort);
        return returnResult(200, recharge, "");
    }


    /**
     * 根据rechId删除支付类型
     * @param rechId
     * @return
     */
    @RequestMapping(value = "/deleteRecharge", method = RequestMethod.GET)
    public ModelAndView deleteRecharge(String rechId){
        int i=iRechargeService.deleteByPrimaryKey(rechId);
        Map<String, Object> data = new HashedMap();
        PageBean<RechargeVO> recharges = iRechargeService.selectRechargeList(PaginationContext.getPageNum(), PaginationContext.getPageSize());
        data.put("rechargeList", recharges);
        return returnView("/rechargeList", data);

    }


}
